
package Objects;

import GameEngine.GameConstants;
import GameEngine.GameConstants.Effect;
import GameEngine.GameConstants.Graphic;
import GameEngine.GameConstants.Material;
import GameEngine.Vector;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author mhfowler
 */
public class Bomb extends GameObject {

    int bombStrength;
            /**
     * A constructor
     * @param dir
     * @param min
     * @param max
     */
    public Bomb(Vector dir, Vector cen, Vector s) {
        super(dir, cen, s, Graphic.META);
        material = Material.BOMB;
        graph = Graphic.MUSHROOM;
        acceleration = new Vector(0,GameConstants.GRAVITY_ACCELERATION);
    }

    public void setStrength(int power) {
        bombStrength = power;
    }

    @Override
    public void receiveEffect(Effect e, Vector m, int power) {
        switch (e) {
            case DESTROY:
                super.receiveEffect(e, m, power);
                break;
            case REVERSE:
                super.receiveEffect(e, m, power);
                break;
            default:
                break;
        }
    }

    @Override
    public void collide(GameObject o) {
        switch (o.material) {
            case BOMB:
                break;
            default:
                    explode();
                break;
        }
    }

    private void explode() {
        if (bombStrength>1) {
            List<Bomb> bombs = new LinkedList<Bomb>();
            Bomb toCreate;
            int deg = 360;
            int unit = 360/bombStrength;
            for (int i=0; i<bombStrength; i++) {
                Vector direc = getDirection(unit*i);
                toCreate = birthBomb(direc);
                bombs.add(toCreate);
            }
            for (Bomb b : bombs) {
                lev.createObject(b);
            }
            receiveEffect(Effect.DESTROY, null, 0);
        }
        else {
            receiveEffect(Effect.DESTROY, null, 0);
        }
    }

    public Bomb birthBomb(Vector dir) {
        Bomb toReturn = new Bomb(dir, new Vector(center), size.scaleNew(0.5));
        toReturn.velocity = dir.scaleNew(5);
        toReturn.setStrength((int) Math.floor(((double)bombStrength)/((double)4.0)));
        return toReturn;
    }


    public Vector getDirection(int degrees) {
        double radians = degrees/180*Math.PI;
        double vx = Math.cos(radians);
        double vy = Math.sin(radians);
        return new Vector(vx, vy);
    }


}
